<?php
class Admin_TourCategoryController extends Apps_Controller_Action_Admin {
	//LIST
	public function indexAction() {
		$this->actResolve('act_delete',
				'delete', null, null, array('id' => $this->_getParam('item_checker')),
				true);

		$tbTourCategory = new M_DbTable_TourCategory();
		$grid = new Apps_Plugin_Grid('tourCategoryGrid');

		$grid->setData($tbTourCategory->getList())
			 ->setTableCaption('Tour Category')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '5%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Title (En)',
			 			  'dataField'	=> 'en_title',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Title (Vi)',
			 			  'dataField'	=> 'title',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Description',
			 			  'dataField'	=> 'description',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Sorting number',
			 			  'dataField'	=> 'sorting_number',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Created Date',
			 			  'dataField'	=> 'created_date',
			 			  'class'		=> 'center'),
			        array('title'		=> 'Show Home',
			              'html'	    => function ($rs) {
                        			                 return ($rs['show_home'] == M_DbTable_TourCategory::SHOW_HOME)?
                        			                 '<a href="##" class="ajax-link-must-confirm popup hint  label label-success" data-ajax-to="/admin/tour-category/change-value?p=show_home&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="No">Yes</a>' :
                        			                 '<a href="##" class="ajax-link-must-confirm popup hint  label label-warning" data-ajax-to="/admin/tour-category/change-value?p=show_home&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="Yes">No</a>';
                        			        },
        			        'class'		=> 'center'),
			 		array('title'      => 'Edit',
			 			  'html'       => array('<a href="/admin/tour-category/edit?id=%s"><i class="icon-edit" title="Sửa Danh Mục"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%')
			 		));
		$this->view->grid = $grid;
	}
	//ADD
	public function addAction() {
		$rq = $this->_request;

		$tbTourCategory = new M_DbTable_TourCategory();
		$f 	= new Admin_Form_AddTourCategory();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbTourCategory->insert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT
	public function editAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbTourCategory = new M_DbTable_TourCategory();

		if(!$id || !is_numeric($id) || !$tbTourCategory->fetchRow(array('id =?' => $id))) {
			$this->_helper->redirector('index', null, null);
		}

		$f 	= new Admin_Form_AddTourCategory();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbTourCategory->update($f->getValues(), array('id =?' => $id))
					? self::$msgUpdateSuccess
					: self::$msgUpdateFailed);
				$this->_helper->redirector('index', null, null);
			}
		}

		$f->setLegend('Edit Tour Category');
		$f->populate($tbTourCategory->fetchRow(array('id =?' => $id))->toArray());
		$this->view->form = $f;
		$this->render('add');
	}

	//DELETE
	public function deleteAction() {
		$this->disableLayout(true, true);

		$id = $this->_getParam('id');
		if (!$id) {
			$this->_helper->redirector('index', null, null);
		}

		$tbTourCategory = new M_DbTable_TourCategory();
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (is_numeric($v)) {
					$tbTourCategory->update(array('status' => $tbTourCategory::STATUS_DELETED),
											array('id =?'  => $v));
				}
			}
		} else if (is_numeric($id)) {
				if ($tbTourCategory->update(array('status' => $tbTourCategory::STATUS_DELETED),
										array('id =?'  => $id)))
				{
					$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
				}
		}
		$this->_helper->redirector('index', null, null);
	}

	public function changeValueAction() {
	    $this->disableLayout(true, true);
	    $rq = $this->_request;
	    $param  = $rq->getParam('p');
	    $id 	= intval($rq->getParam('id'));
	    $tbTourCategory = new M_DbTable_TourCategory();
	    if(!($r = $tbTourCategory->fetchRow(array('id = ?' => $id))->toArray())) {
	        echo '<script>window.location=window.location</script>
	        <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	        . $this->getBacktrackUrl()
	        . '">Update Error (Record not found), please wait ...</a>';
	    }

	    switch ($param) {
	        case 'show_home':
	            ($r['show_home'] == M_DbTable_TourCategory::SHOW_HOME)
	            ? $tbTourCategory->safelyUpdate(array('show_home' 		=> M_DbTable_TourCategory::NOT_SHOW_HOME),
                            	                array('id =?'			=> $id))
                : $tbTourCategory->safelyUpdate(array('show_home' 		=> M_DbTable_TourCategory::SHOW_HOME),
                            	                array('id =?'			=> $id));
	            break;

	        case 'public':
	            ($r['status'] == M_DbTable_TourCategory::STATUS_ACTIVE)
	            ? $tbTourCategory->safelyUpdate(array('status'  => M_DbTable_TourCategory::STATUS_INACTIVE),
	                                            array('id =?'	  => $id))
                : $tbTourCategory->safelyUpdate(array('status'  => M_DbTable_TourCategory::STATUS_ACTIVE),
	                                            array('id =?'  => $id));
	            break;


	        default: break;
	    }

	    echo '<script>document.location.reload()</script>
	    <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	    . $this->getBacktrackUrl()
	    . '">Update successfully, please wait ...</a>';
	}

}